﻿  var CSP_HeadNavigation = {
        Data: undefined,
        Init: function () {
            this.CreateCategoryHtml();
        },
        //获取Category数据
        GetCategoryData: function () {
            var that = this;
            if (this.Data != undefined) {
                return that.Data;
            }
            $.ajax({
                dataType: "json",
                type: "get",
                async: false,
                url: window.location.pathname+"?getcategorydata=1",
                success: function (data, textStatus, jqXHR) {
                    that.Data = data;
                },
                error: function (result) {
                    result;
                }
            });
            return that.Data;
        },
        //创建分类的html 内容
        CreateCategoryHtml: function () {
            var categoryData = this.GetCategoryData();
            var titleData = this.GetTitleDataByCategoryData(categoryData);
            var lastLevelData = this.GetLastLevelByCategoryData(categoryData);

            var ul = $(".mcate-bd");
            var lis = "";
            $.each(titleData, function (index, data) {
                var classSuffix = "";
                if (index == 0) {
                    classSuffix = "first";
                }
                var temli = '<li class="item ' + classSuffix + '"><h3 class="mcate-item-hd"><a href="/search/st' + data.Id + '/" target="_blank">' + data.Name + '</a></h3>';
                temli += '<p class="mcate-item-bd">';
                $.each(lastLevelData, function (lastLevelIndex, lastLevelData) {
                    if (lastLevelData.ParentId == data.Id) {
                        temli += '<a href="/search/st' + lastLevelData.Id + '/" target="_blank">' + lastLevelData.Name + '</a> ';
                    }

                });
                temli += '</p</li>';
                lis += temli;
            });
            ul.append(lis);

        },
        //根据分类数据 获取头部的分类数据
        GetTitleDataByCategoryData: function (categoryData) {
            var result = [];
            var temFirstLevel = [];
            var i = 0;
            var j = 0;
            $.each(categoryData, function (index, data) {
                if (data.Level == 2) {
                    result[i] = data;
                    i++;
                }
                if (data.Level == 1) {
                    temFirstLevel[j] = data;
                    j++;
                }
            });

            //根据第一级别的OrderId 跟第二季的OrderId 进行组合排序
            $.each(result, function (index, data) {
                var orderId = 0;
                $.each(temFirstLevel, function (firstLevelIndex, firstLevelData) {
                    if (data.ParentId == firstLevelData.Id) {
                        orderId = firstLevelData.OrderId;
                    }
                });
                result.OrderId = parseInt(orderId + "" + result.OrderId);

            });

            //排序
            result.sort(function (a, b) {
                return a.OrderId - b.OrderId;
            });
            return result;
        },
        //根据分类数据获分类的最低级别数据
        GetLastLevelByCategoryData: function (categoryData) {
            var result = [];
            var i = 0;
            $.each(categoryData, function (index, data) {
                if (data.Level = 3) {
                    result[i] = data;
                    i++;
                }
            });

            //排序
            result.sort(function (a, b) {
                return a.OrderId - b.OrderId;
            });
            return result;
        }
    };
